package com.example.ruijiproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ruijiproject.domain.Employee;
import com.example.ruijiproject.domain.Result;
import com.example.ruijiproject.service.EmployeeService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j //lombok的日志注解
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登入功能
     * @param request
     * @param employee
     * @return
     */
    //发送post请求
    @PostMapping("/login")
    public Result<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //这部分就是MP
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lqw);
        if (emp == null) {
            return Result.error("登陆失败");
        }
        if (!emp.getPassword().equals(password)) {
            return Result.error("登录失败");
        }
        if (emp.getStatus() == 0) {
            return Result.error("该用户已被禁用");
        }
        //存个Session，只存个id就行了
        request.getSession().setAttribute("employee",emp.getId());
        return Result.success(emp);
    }

    /**
     * 登出功能
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return Result.success("退出成功");
    }

    /**
     * 添加员工
     */
    @PostMapping
    public Result<String> save(HttpServletRequest request,@RequestBody Employee employee) {
        //设置初始密码为123456，采用md5进行密码加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //存入数据库
        employeeService.save(employee);
        return Result.success("添加成功");
    }

    /**
     * 分页查询员工
     */
    @GetMapping("/page")
    public Result<Page> page(int page, int pageSize, String name) {
        log.info("当前页：{}，每页条数：{}，查询条件：{}",page,pageSize,name);
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(!(name == null || "".equals(name)),Employee::getName, name);
        lqw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo, lqw);
        return Result.success(pageInfo);
    }

    /**
     * 删除员工
     */
    @PutMapping
    public Result<String> update(HttpServletRequest request,@RequestBody Employee employee) {
        log.info("修改的员工信息：{}",employee);
        long ThreadId = Thread.currentThread().getId();
        log.info("PutMapping的线程id为：{}", ThreadId);
        employeeService.updateById(employee);
        return Result.success("修改员工信息成功");
    }

    /**
     * 获取员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> get(@PathVariable Long id) {
        log.info("查询的员工id：{}",id);
        Employee employee = employeeService.getById(id);
        if (employee == null) {
            return Result.error("查询失败");
        }
        return Result.success(employee);
    }

}